using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace CatalogManagerWebApp
{
    public partial class ListEntities : System.Web.UI.Page
    {
        private CatalogManagerDAL.ListEntities DAL_ListEntities = new CatalogManagerDAL.ListEntities();
        private CatalogManagerDAL.Translator DAL_Translator = new CatalogManagerDAL.Translator();
        

        private string EntityType = string.Empty;

        public ListEntities()
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // prepare
                if (this.IsPostBack) return;
                if (Request.QueryString["EntityType"] != null) EntityType = Request.QueryString["EntityType"];

                string CatalogName = string.Empty;
                if (Request.QueryString["CatalogName"] != null) CatalogName = Request.QueryString["CatalogName"];

                string CategoryName = string.Empty;
                if (Request.QueryString["CategoryName"] != null) CategoryName = Request.QueryString["CategoryName"];


                switch (EntityType)
                {
                    case "ProductDefinitions":
                        ListProductDefinitions(CatalogName, CategoryName);
                        break;
                    case "ProductDefinitionFields":
                        ListProductDefinitionFields();
                        break;
                    case "Catalogs":
                        ListCatalogs();
                        break;
                    case "PropertyAttributes":
                        ListProductDefinitionPropertyAttributes(); 
                        break;
                    case "CatalogsCategories":
                        ListCategories(CatalogName);
                        break;
                    case "CategoriesCategories":
                        ListCategories(CatalogName, CategoryName);
                        break;
                    case "Products":
                        ListProducts(CatalogName, CategoryName);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CatalogManagerDAL.Logger.Log(ex.Message);
            }
        }

        private void ListProducts(string CatalogName, string CategoryName)
        {
            DataView dview = DAL_ListEntities.ListProducts(CatalogName, CategoryName).DefaultView;

            grdEntityList.AutoGenerateColumns = false;
            // PurchasePrice, OriginalPrice, MarketPrice, ExchangeRate, ProductName, Rank, DefinitionName
            grdEntityList.Columns.Add(
                CreateBoundColumn("DisplayName", "DisplayName",
                    "/UpdateEntitiesTabbed.aspx?EntityType=Product&CatalogName=" + CatalogName + "&CategoryName=" + CategoryName + "&ProductId={0}&VariantId={1}&ProductDefinition={2}",
                    new string [] {
                        "ProductId", "VariantId", "DefinitionName"
                    }
                )
            );
            grdEntityList.Columns.Add(CreateBoundColumn("cy_list_price", "cy_list_price"));
            grdEntityList.Columns.Add(CreateBoundColumn("ExchangeRate", "ExchangeRate"));
            grdEntityList.Columns.Add(CreateBoundColumn("IsVisible", "IsVisible"));
            grdEntityList.Columns.Add(CreateBoundColumn("IsApproved", "IsApproved"));
            grdEntityList.Columns.Add(CreateBoundColumn("ProductId", "ProductId"));
            grdEntityList.Columns.Add(CreateBoundColumn("VariantId", "VariantId"));
            
            grdEntityList.DataSource = dview;
            grdEntityList.DataBind();
        }

        private DataControlField CreateBoundColumn(string Title, string DataFieldName)
        {
            BoundField col = new BoundField();
            col.DataField = DataFieldName;
            col.HeaderText = Title;
            return col;
        }
        private DataControlField CreateBoundColumn(string Title, string DataFieldName, string DataNavigateUrlFormatString, string [] DataNavigateUrlFields)
        {
            HyperLinkField col = new HyperLinkField();
            col.DataTextField = DataFieldName;
            col.HeaderText = Title;
            // col.NavigateUrl = Url;
            col.DataNavigateUrlFields = DataNavigateUrlFields;
            col.DataNavigateUrlFormatString = DataNavigateUrlFormatString;
            return col;
        }

        private void ListCategories(string CatalogName, string CategoryName)
        {
            grdEntityList.DataSource = DAL_ListEntities.ListCategories(CatalogName, CategoryName).DefaultView;
            grdEntityList.DataBind();
        }

        private void ListCategories(string CatalogName)
        {
            grdEntityList.DataSource = DAL_ListEntities.ListCategories(CatalogName, null).DefaultView;
            grdEntityList.DataBind();
        }

        private void ListProductDefinitionPropertyAttributes()
        {
            // throw new Exception("The method or operation is not implemented.");
            DataRow resultRow = DAL_ListEntities.GetProductDefinitionPropertyAttributes("PropertyName='Desi'");
            DataTable dt = resultRow.Table.Clone();
            dt.ImportRow(resultRow);
            dt.AcceptChanges();

            grdEntityList.DataSource = dt.DefaultView;
            grdEntityList.DataBind();

        }

        private void ListCatalogs()
        {
            DataTable ResultSet = DAL_ListEntities.ListCatalogs();
            grdEntityList.DataSource = ResultSet.DefaultView;
            grdEntityList.DataBind();
        }

        private void ListProductDefinitionFields()
        {
            try
            {
                // execute
                DataTable ResultSet = DAL_ListEntities.ListSingleProductDefinitionsProperties("");
                if (ResultSet.Rows.Count > 0)
                {
                    ResultSet.DefaultView.Sort = "DefinitionName";
                    grdEntityList.DataSource = ResultSet.DefaultView;
                    grdEntityList.DataBind();
                }

                if (EntityType.Length > 0) lblEntityType.Text = DAL_Translator.DictionaryTR[EntityType];
            }
            catch (Exception ex)
            {
                
                // Logger.Log(ex.Message);
                throw ex;
            }
        }

        private void ListProductDefinitions(string CatalogName, string CategoryName)
        {
            try
            {
                // execute
                DataSet ResultSet = DAL_ListEntities.ListProductDefinitions();
                if (ResultSet.Tables.Count > 0 && ResultSet.Tables[0].Rows.Count > 0)
                {
                    grdEntityList.AutoGenerateColumns = false;

                    System.Web.UI.WebControls.HyperLinkField tmpCol = new HyperLinkField();
                    tmpCol.DataTextField = "DefinitionName";
                    tmpCol.DataNavigateUrlFields = new string[] { "DefinitionName" };
                    tmpCol.DataNavigateUrlFormatString = String.Format(
                        "/CreateEntities.aspx?EntityType=Product&CatalogName={0}&CategoryName={1}", 
                        CatalogName, CategoryName
                    )+"&ProductDefinition={0}";

                    grdEntityList.Columns.Add(tmpCol);

                    grdEntityList.DataSource = ResultSet.Tables[0].DefaultView;
                    grdEntityList.DataBind();
                }

                if (EntityType.Length > 0) lblEntityType.Text = DAL_Translator.DictionaryTR[EntityType];
            }
            catch (Exception ex)
            {
                // Logger.Log(ex.Message);
                throw ex;
            }
        }

        protected void grdEntityList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (Request.QueryString["EntityType"] == "Products")
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // Display the company name in italics.
                    //e.Row.Cells[0].Text = String.Format(
                    //    "<a href=\"/UpdateEntitiesTabbed.aspx?EntityType=Product&CatalogName={0}&ProductId={1}&VariantId={2}\">{3}</a>",
                    //    e.Row.Cells[0].Text, e.Row.Cells[1].Text, e.Row.Cells[2].Text, e.Row.Cells[3].Text
                    //    // e.Row.Cells["ProductId"].Text, e.Row.Cells["VariantId"].Text, e.Row.Cells["ProductId"].Text
                    //);
                }
            }
        }

    }
}
